package com.github.iot.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.github.iot.R;
import com.github.iot.entity.User;

import java.util.List;

/**
 * 用户列表适配器
 *
 * @Author: 梁声洪
 * @Date: 2019/12/10 22:10
 * @Version: v1.0.0
 */
public class UserRecycleAdapter extends RecyclerView.Adapter<UserRecycleAdapter.UserViewHolder> {

    private Context context;
    private List<User> list;
    private View inflater;

    public UserRecycleAdapter(Context context, List<User> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
        UserViewHolder viewHolder = new UserViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        User user = list.get(position);
        holder.mNickname.setText(user.getNickname());
        holder.mUsername.setText(user.getUsername());
        if (1 == user.getType()) {
            holder.mType.setText("[超级管理员]");
        } else if (2 == user.getType()) {
            holder.mType.setText("[管理员]");
        } else if (3 == user.getType()) {
            holder.mType.setText("[普通用户]");
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class UserViewHolder extends RecyclerView.ViewHolder {
        TextView mNickname;
        TextView mUsername;
        TextView mType;

        public UserViewHolder(View itemView) {
            super(itemView);
            mNickname = itemView.findViewById(R.id.tv_nickname);
            mUsername = itemView.findViewById(R.id.tv_username);
            mType = itemView.findViewById(R.id.tv_type);
        }
    }

}